package com.yjs.app.trade.web.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.yjs.app.trade.core.util.MongoUtil;
import com.yjs.app.trade.entity.base.BusinessConstant;
import com.yjs.app.trade.service.service.business.member.MemberService;
import com.yjs.app.trade.service.service.business.search.NewSearchListService;
import com.yjs.app.trade.service.service.sys.AppVersionService;
import com.yjs.app.trade.service.service.util.FastDFSUtil;
import com.yjs.app.trade.service.service.util.KafkaService;
import com.yjs.app.trade.service.service.util.search.SearchConditionUtil;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;
import com.yjs.list.model.ListParamBase;

@Controller
@RequestMapping(value = "test")
public class TestController extends BaseController {
	@Autowired
	MemberService memberService;
	@Autowired
	AppVersionService appVersionService;
	@Autowired
	KafkaService kafkaService;
	
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test() {
		return "index";
	}
	
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public String download() {
		return "download/download";
	}
	
	@RequestMapping(value = "/androidVersion", method = RequestMethod.GET)
	public Object androidVersion(HttpServletRequest request) {
		String localUrl = request.getParameter("localUrl");
		String url = localUrl + "resources/html/dist/index.html#/" + request.getParameter("type") + "?" + request.getQueryString();
//		return appVersionService.getAndroidLastVersion();
		return "redirect:" + url;
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Object sendPhoneCode(MultipartHttpServletRequest request) {
		Iterator<String> itr = request.getFileNames();
		MultipartFile file;
		List<String> list = new ArrayList<String>();
		while (itr.hasNext()) {
			try {
				file = request.getFile(itr.next());
				String fileName = file.getOriginalFilename();
				if (fileName.contains(".png") || fileName.contains(".mp4") || fileName.contains(".jpg") || fileName.contains(".jpeg") || 
						fileName.contains(".bmp") || fileName.contains(".gif")) {
					String fileUrl = FastDFSUtil.uploadFile(file);
					list.add(fileUrl);
				} else {
					return Result.fail("","不能上传该格式的文件,只能上传mp4、png、jpg、jpeg等常见格式的图片文件");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Result.success(list);
	}
	
	@Autowired
	NewSearchListService newSearchListService;
	
	@RequestMapping(value = "/mongoDBTest", method = RequestMethod.GET)
	@ResponseBody
	public Object mongoDBTest(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String mongoDbCollection = "";
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String string = (String) enumeration.nextElement();
			if (string.equals("collection")) {
				mongoDbCollection = request.getParameter(string);
			} else {
				map.put(string, request.getParameter(string));
			}
		}
		System.out.println(map);
		return Result.success(MongoUtil.getListInfo(mongoDbCollection, map, null, 1, 10, null));
		 
	}
	
	@RequestMapping(value = "/findCQRegion")
	@ResponseBody
	public Object findCQRegio(HttpServletResponse response) {
		return Result.success(memberService.getRegionListSort("500000"));
	}
	
/*	@RequestMapping(value = "/solrTest")
	@ResponseBody
	public Object solrTest(HttpServletResponse response) {
		List<Order> orders = new ArrayList<>();
		List<ListParamBase> listParamBases = new ArrayList<ListParamBase>();
		ListParamBase paramBase = new ListParamBase();
		paramBase.setFieldName("DRUG_CODE");
		paramBase.setValue1("103507");
		paramBase.setFieldTypeId("");
		paramBase.setEnumSearchType(enumSearcheType);
		listParamBases.add(paramBase);
		listParamBases.add(SearchConditionUtil.getParamBaseValue("REFUSE_TIME", "",
				"String", "neq"));
		
		return newSearchListService.listSearchIndexs(
				SearchConditionUtil.searchMap(BusinessConstant.Number.DRAGORDER, "1424", 1, listParamBases, 1, 10, orders));
	}*/
}
